/**
 * Created by vincent on 2017/9/20.
 */
moduleStudyMap.controller("SetGaugeController", function ($$CambioConfig, $scope, $log, $state, LoadingService, DialogService,
                                                          EnumService, ConstantService, StudyClassResource, GaugeResource) {
    $scope.$on("ngDialog.opened", function () {
        dialogOnLoad();
        getGaugeStudyClass();
    });

    var getGaugeStudyClass = function () {
        if (StringUtils.isNlOrUndOrEmpty($scope.currentStudyClass.targetId)) {
            $scope.setGaugeStudyClass = {
                gaugeTemplate: {}
            };
        } else {
            //LoadingService.show();
            StudyClassResource.getTarget({studyClassId: $scope.currentStudyClass.id}, function (gaugeStudyClassResult) {
                $log.debug("课程相关量表信息", gaugeStudyClassResult);
                $scope.setGaugeStudyClass = gaugeStudyClassResult;
                //LoadingService.hide();
            }, function () {
                //LoadingService.hide();
            });
        }
    };

    $scope.queryGauge = function () {
        //LoadingService.show();
        GaugeResource.findByKey({}, {key: $scope.gaugeKey}, function (gaugeResult) {
            $log.debug("量表匹配结果", gaugeResult);
            $scope.gaugeResult = gaugeResult;
            //LoadingService.hide();
        });
    };

    $scope.setCurrentGauge = function (gauge) {
        $scope.setGaugeStudyClass.gaugeTemplate = {
            id: gauge.gaugeTemplateId,
            name: gauge.gaugeTemplateName,
            code: gauge.gaugeTemplateCode
        };
    };

    $scope.cancel = function () {
        DialogService.close();
    };

    $scope.submit = function () {
        if ($scope.setGSCForm.$valid) {
            if (!StringUtils.isNlOrUndOrEmpty($scope.setGaugeStudyClass.gaugeTemplate.id)) {
                //LoadingService.show();
                StudyClassResource.setGauge({studyClassId: $scope.currentStudyClass.id}, $scope.setGaugeStudyClass, function (studyClassResult) {
                    $log.debug("设置课程量表结果", studyClassResult);
                    angular.copy(studyClassResult, $scope.currentStudyClass);
                    DialogService.close();
                    //LoadingService.hide();
                    Alert.success("设置量表成功！");
                }, function () {
                    //LoadingService.hide();
                });
            } else {
                Alert.error("课程的量表信息不可为空！");
            }
        }
    };

});